Skip to content

Method: storeRoundData(IGameMemoryIdentifier, Object)

1: package de.fhdw.gaming.ipspiel23.memory;
2:
3: import java.util.Optional;
4:
5: import de.fhdw.gaming.core.domain.Player;
6: import de.fhdw.gaming.core.domain.State;
7:
8: /**
9: * Represents the base class for all game states offering support for memory management
10: * <p>
11: * Provides access to the {@link IGameMemoryProvider} that is used to store and retrieve decisions and round data.
12: * </p>
13: * @param <P> The player type.
14: * @param <S> The state type.
15: * @param <ROUND_DATA> The round data type.
16: */
17: public abstract class MemoryState<P extends Player<P>, S extends State<P, S>, ROUND_DATA>
18: implements IMemoryState<P, S, ROUND_DATA> {
19:
20: /**
21: * needs to be preserved across states/rounds/games
22: * Don*t know how to keep them otherwise, so what was the exact problem with the static solution?
23: */
24: private final IGameMemoryProvider memoryProvider = GameMemoryProvider.getInstance();
25:
26: @Override
27: public IGameMemoryProvider getMemoryProvider() {
28: return this.memoryProvider;
29: }
30:
31: /**
32: * Stores the given round data in the memory for the given identifier,
33: * if there is a memory instance associated with the given identifier.
34: * There may be strategies that do not need a memory
35: * so we don't want to create a memory for those strategies
36: * only request a memory if there is one
37: * @param identifier The identifier uniquely describing the memory instance
38: * for a given player and strategy combination.
39: * @param roundData The round data to store.
40: */
41: protected void storeRoundData(final IGameMemoryIdentifier identifier, final ROUND_DATA roundData) {
42: final Optional<IGameMemory<ROUND_DATA>> memory = memoryProvider.tryRequestMemoryForStrategy(identifier);
43:• if (memory.isPresent()) {
44: memory.get().add(roundData);
45: }
46: }
47: }